package crew.blt.handsound.client.widgets.user;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.rpc.AsyncCallback;

import crew.blt.handsound.client.services.user.UserService;
import crew.blt.handsound.client.services.user.UserServiceAsync;
import crew.blt.handsound.shared.domain.User;
import crew.gwt.client.domain.OrderedEntitiesResponse;
import crew.gwt.client.services.util.ServerCall;
import crew.gwt.client.widgets.util.RichEntityGrid;

public class UserEntityGrid extends RichEntityGrid<User>{

	
	public @UiConstructor UserEntityGrid(boolean autoLoad) {
		super(new User());
		if(autoLoad){
			loadEntities();
		}
	}
	
	@Override
	protected void sortGrid(final int index, final boolean asc, final int startIndex,final int gridCapacity) {
		final UserServiceAsync service = GWT.create(UserService.class);
		new ServerCall<OrderedEntitiesResponse<User>>() {

			@Override
			protected void callService(AsyncCallback<OrderedEntitiesResponse<User>> asyncCallback) {
				service.getOrderedEntities(index, asc, gridCapacity, startIndex, asyncCallback);
			}

			@Override
			public void onSuccess(OrderedEntitiesResponse<User> list) {
				clear();
				addEntities(list.getEntities());
				setEntitiesNb(list.getRowCt());
			}
		}.call();
//		Window.alert("TODO: Call users ");
	}
}
